<?php

class Publicar_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    public function guardaOferta($opc, $usuario, $usuarioId) {
        switch ($opc) {
            case '1': //ofertaLaboral
                if ($this->verificaUsr("empresas", $usuarioId)) {
                    $usuario = explode("|", $usuario);
                    //$usuario[0]   -> Id del Rol
                    //$usuario[1]   -> Id de la tabla del usuario
                    //$usuario[2]   -> Nombre
                    $val = "";
                    $sqlAux = "";
                    if ($this->input->post("prioInca") != "No") {
                        $val = ",'{$this->input->post("prioInca")}'";
                        $sqlAux = ",tipDiscapacidadId";
                    }
                    $sql = "insert into ofertaslaborales(ofertaLaboralCargo,ofertaLaboralDescripcion,jornadaId,ofertaLaboralPerfil,ofertaLaboralContrato,ofertaLaboralRangoSalarioBase,ofertaLaboralRangoSalarioTope,ofertaLaboralVacantes,ofertaLaboralAccesibilidad,ofertaLaboralFechaCreo,ofertaLaboralFechaCierre,empresaId,ciudadId,`competenciaId`,ofertaLaboralInicia,ofertaLaboralDuracion,ofertaLaboralMostrarInfo $sqlAux) values(";
                    $sql .= "'{$this->input->post("cargo")}','{$this->input->post("descrip")}','{$this->input->post("jornada")}','{$this->input->post("perfil")}','{$this->input->post("contrato")}','{$this->input->post("salbase")}','{$this->input->post("saltope")}','{$this->input->post("vacantes")}','{$this->input->post("accesibilidad")}',now(),'{$this->input->post("fecCierre")}','{$usuario[1]}','{$this->input->post("ciudad")}','{$this->input->post("competencias")}','{$this->input->post("inicia")}','{$this->input->post("duracion")}','{$this->input->post("permitirAcceso")}' $val)";
                    $this->db->query($sql);
                    return true;
                }
                else
                    return false;
                break;
            case '2': //oferta de Servicios
                switch ($this->session->userdata("rolId")) {
                    case '2': //empresa
                        if ($this->verificaUsr("empresas", $usuarioId)) {
                            $usuario = explode("|", $usuario);
                            //$usuario[0]   -> Id del Rol
                            //$usuario[1]   -> Id de la tabla del usuario
                            //$usuario[2]   -> Nombre
                            $servicios = serialize($this->input->post("formacion"));
                            $sql = "insert into solservyase(solServyAseTipo,solServyAseDescripcion,solServyAseFechaCreo,solServyAseFechaCierre,empresaId,solServyAseNombre,ciudadId,solServyAseSalBase,solServyAseSalTope,solServyAseServicios) values(";
                            $sql .= "'{$this->input->post("tipServ")}','{$this->input->post("descrip")}',now(),'{$this->input->post("fecCierre")}','{$usuario[1]}','{$this->input->post("nombre")}','{$this->input->post("ciudad")}','{$this->input->post("salbase")}','{$this->input->post("saltope")}','$servicios')";
                            $this->db->query($sql);
                            return true;
                        }
                        else
                            return false;
                        break;
                    case '4': //persona
                        if ($this->verificaUsr("personas", $usuarioId)) {
                            $this->db->trans_start(); //inicia la transaccion
                            $servicios = serialize($this->input->post("formacion"));
                            $sql = "insert into solservyase(solServyAseTipo,solServyAseDescripcion,solServyAseFechaCreo,personasId,solServyAseNombre,ciudadId,solServyAseServicios) values(";
                            $sql .= "'{$this->input->post("tipServ")}','{$this->input->post("descrip")}',now(),'$usuario','{$this->input->post("nombre")}','{$this->input->post("ciudad")}','$servicios')";
                            $this->db->query($sql);
                            $solId = $this->db->insert_id();
                            $sql = "insert into postulaciones(postulacionFecha,postulacionPreseleccion,postulacionFechaPreseleccion,institucionId,solServyAseId,personasId) value(";
                            $sql .= "now(),'Si',now(),'{$this->input->post("inpHidden")}','$solId','{$this->session->userdata("tablaUsuarioId")}')";
                            $this->db->query($sql);
                            $postuId = $this->db->insert_id();
                            $sql = "insert into contratos(contratoFecha,postulacionesId) values(";
                            $sql .= "now(),'$postuId')";
                            $this->db->query($sql);
                            $this->db->trans_complete();
                            return true;
                        }
                        else
                            return false;
                        break;
                }
        } //fin switch ($opc) {
        return;
    }

    public function verificaUsr($opc, $val) {
        $sql = "select usuarioId from $opc where usuarioId = '$val'";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0)
            return true;
        else
            return false;
    }

    public function buscaCiudad($dep) {
        $sql = "select * from ciudades where dptCod = '$dep' order by ciudad";
        return $this->db->query($sql);
    }

    public function guarda($rolId, $accion = Null) {
        $validaExtra = ($accion == "EDITA") ? " and usuarioId <> '{$this->input->post('usuarioId')}'" : "";
        //valida que no exista el usuario
        $sql = "select * from usuarios where usuario = '{$this->input->post('email')}' $validaExtra";
        $query = $this->db->query($sql);
        if ($this->db->query($sql)->num_rows() > 0)
            return "0";
        //Valida en la tabla correspondiente
        switch ($rolId) {
            case '2'://Empresa
                //valida que la empresa no exista ya en el sistema
                $validaExtra = ($accion == "EDITA") ? " and empresaId <> '{$this->input->post('empId')}'" : "";
                $sql = "select * from empresas where empresaNit = '{$this->input->post('nit')}' $validaExtra";
                if ($this->db->query($sql)->num_rows() > 0)
                    return "1";
                break;
            case '3'://Institucion
                //Valida que la institucion no exista en el sistema
                $validaExtra = ($accion == "EDITA") ? " and institucionId <> '{$this->input->post('instiId')}'" : "";
                $sql = "select * from instituciones where institucionNit = '{$this->input->post('nit')}' $validaExtra";
                if ($this->db->query($sql)->num_rows() > 0)
                    return "1";
                break;
            case '4'://Persona
                //Valida que la persona no exista en el sistema
                $validaExtra = ($accion == "EDITA") ? " and personasId <> '{$this->input->post('personaId')}'" : "";
                $sql = "select * from personas where personaDocumento = '{$this->input->post('nit')}' and personaTipoDocumento = '{$this->input->post('tipDoc')}' $validaExtra";
                if ($this->db->query($sql)->num_rows() > 0)
                    return "1";
                break;
            default:
                break;
        }
        $this->db->trans_start(); //inicia la transaccion
        if ($accion == "") {
            //FASE DE INSERTS DESPUES DE LAS VALIDACIONES
            //Guarda primero el usuario
            $sql = "insert into usuarios(usuario,usuarioClave,rolId) values(";
            $sql .= "'{$this->input->post('email')}',md5('{$this->input->post('passwd')}'),'{$rolId}')";
            $this->db->query($sql);
            $lastId = $this->db->insert_id();
            //Guarda en la tabla correspondiente
            switch ($rolId) {
                case '2'://Empresa
                    $sql = "insert into empresas(empresaRazon,empresaNit,empresaTelefono1,empresaDireccion,empresaPagina,empresaContacto,empresaFechaCreo,sectorId,ciudadId,usuarioId) values(";
                    $sql .= "'{$this->input->post('razon')}','{$this->input->post('nit')}','{$this->input->post('telefonos')}','{$this->input->post('direccion')}','{$this->input->post('pagina')}','{$this->input->post('contacto')}',now(),'{$this->input->post('sector')}','{$this->input->post('ciudad')}','$lastId')";
                    $this->db->query($sql);
                    break;
                case '3'://Institucion
                    $sql = "insert into instituciones(institucionRazon,institucionNit,institucionTelefono,institucionDireccion,institucionPagina,institucionContacto,institucionTipoFormacion,institucionEnfoquesFormacion,institucionFechaCreo,ciudadId,usuarioId) values(";
                    $sql .= "'{$this->input->post('razon')}','{$this->input->post('nit')}','{$this->input->post('telefonos')}','{$this->input->post('direccion')}','{$this->input->post('pagina')}','{$this->input->post('contacto')}','{$this->input->post('formacion')}','{$this->input->post('enfoque')}',now(),'{$this->input->post('ciudad')}','$lastId')";
                    $this->db->query($sql);
                    break;
                case '4'://Persona
                    $tran = $this->input->post("transaccion");
                    $sql = "insert into personas(personaDocumento,personaTipoDocumento,personaCompetencias,personaNivelAcademico,personaNombres,personaApellidos,personaGenero,personaFechaNac,personaEstadoCivil,personaPersonaACargo,personaEstrato,ciudadId,personaAspiracion,personaFechaCreo,usuarioId) values(";
                    $sql .= "'{$this->input->post('nit')}','{$this->input->post('tipDoc')}','{$this->input->post('competencias')}','{$this->input->post('nivelAca')}','{$this->input->post('nombre')}','{$this->input->post('apellido')}','{$this->input->post('genero')}','{$this->input->post('fecNac')}','{$this->input->post('estadoCivil')}','{$this->input->post('perCargo')}','{$this->input->post('estrato')}','{$this->input->post('ciudad')}','{$this->input->post('aspiracion')}',now(),'$lastId')";
                    $this->db->query($sql);
                    $personaId = $this->db->insert_id();
                    //Revisa si ingreso experiencia Laboral
                    if ($this->countTemporal("exp", $tran) > 0) {
                        $sql = "select tempVarchar1,tempVarchar2,tempVarchar3,tempVarchar4,tempDate1,tempDate2,tempText1 from temporal where transaccion = '$tran' and tempInd = 'exp'";
                        $query = $this->db->query($sql);
                        foreach ($query->result() as $value) {
                            $sql = "insert into explaboral(expLaboralEmpresa,expLaboralContacto,expLaboralTelefono,expLaboralInicio,expLaboralFin,expLaboralFunciones,expLaboralActual,personasId) values (";
                            $sql .= "'{$value->tempVarchar1}','{$value->tempVarchar3}','{$value->tempVarchar4}','{$value->tempDate1}','{$value->tempDate2}','{$value->tempText1}','{$value->tempVarchar2}','$personaId')";
                            $this->db->query($sql);
                        }
                    }
                    //Revisa si ingreso Información Académica
                    if ($this->countTemporal("aca", $tran) > 0) {
                        $sql = "select tempVarchar1,tempVarchar2,tempVarchar4,tempVarchar3,tempVarchar5,tempDate1,tempDate2 from temporal where transaccion = '$tran' and tempInd = 'aca'";
                        $query = $this->db->query($sql);
                        foreach ($query->result() as $value) {
                            $sql = "insert into formacademicas(formAcademicaTipo,tipFormacionId,formAcademicaInstitucion,formAcademicaTitulo,formAcademicaInicio,formAcademicaFin,formAcademicaActual,personasId) values (";
                            $sql .= "'{$value->tempVarchar2}','{$value->tempVarchar3}','{$value->tempVarchar1}','{$value->tempVarchar4}','{$value->tempDate1}','{$value->tempDate2}','{$value->tempVarchar5}','$personaId')";
                            $this->db->query($sql);
                        }
                    }
                    //Revisa si ingreso Discapacidades
                    if ($this->countTemporal("discapacidad", $tran) > 0) {
                        $sql = "select tempVarchar1,tempText1,tempText2 from temporal where transaccion = '$tran' and tempInd = 'discapacidad'";
                        $query = $this->db->query($sql);
                        foreach ($query->result() as $value) {
                            $sql = "insert into discapacidades(discapacidadDesc,tipDiscapacidadId,discapacidadCausa,personasId) values (";
                            $sql .= "'{$value->tempText1}','{$value->tempVarchar1}','{$value->tempText2}','$personaId')";
                            $this->db->query($sql);
                        }
                    }
                    //Revisa si ingreso Ayudas Tecnicas
                    if ($this->countTemporal("ayudasTecnicas", $tran) > 0) {
                        $sql = "select tempVarchar2,tempText1 from temporal where transaccion = '$tran' and tempInd = 'ayudasTecnicas'";
                        $query = $this->db->query($sql);
                        foreach ($query->result() as $value) {
                            $sql = "insert into ayudastecnicas(tipAyudaTecnicaId,ayudaTecnicaComentario,personasId) values (";
                            $sql .= "'{$value->tempVarchar2}','{$value->tempText1}','$personaId')";
                            $this->db->query($sql);
                        }
                    }
                    //Revisa si ingreso Seguridad Social
                    if ($this->countTemporal("administraPersonas", $tran) > 0) {
                        $sql = "select tempInt1,tempVarchar2,tempDate1,tempDate2,tempVarchar3 from temporal where transaccion = '$tran' and tempInd = 'administraPersonas'";
                        $query = $this->db->query($sql);
                        foreach ($query->result() as $value) {
                            $sql = "insert into adminispersona(administradoraId,adminPersonaContrato,adminPersonaFecIni,adminPersonaFecFin,adminPersonaActual,personasId) values (";
                            $sql .= "'{$value->tempInt1}','{$value->tempVarchar2}','{$value->tempDate1}','{$value->tempDate2}','{$value->tempVarchar3}','$personaId')";
                            $this->db->query($sql);
                        }
                    }
                    break;
            }
        }//fin if($accion == ""){
        elseif ($accion == "EDITA") {
            //Guarda primero el usuario
            $sql = "update usuarios set usuario = '{$this->input->post('email')}' where usuarioId = '{$this->input->post('usuarioId')}'";
            $this->db->query($sql);
            switch ($rolId) {
                case '2'://Empresa
                    $sql = "update empresas set ";
                    $sql .= "empresaRazon   = '{$this->input->post('razon')}',";
                    $sql .= "empresaNit     = '{$this->input->post('nit')}',";
                    $sql .= "empresaTelefono1= '{$this->input->post('telefonos')}',";
                    $sql .= "empresaDireccion= '{$this->input->post('direccion')}',";
                    $sql .= "empresaPagina  = '{$this->input->post('pagina')}',";
                    $sql .= "empresaContacto= '{$this->input->post('contacto')}',";
                    $sql .= "sectorId       = '{$this->input->post('sector')}',";
                    $sql .= "ciudadId       = '{$this->input->post('ciudad')}' where empresaId = '{$this->input->post('empId')}'";
                    $this->db->query($sql);
                    break;
                case '3'://Institucion
                    $sql = "update instituciones set ";
                    $sql .= "institucionRazon       = '{$this->input->post('razon')}',";
                    $sql .= "institucionNit         = '{$this->input->post('nit')}',";
                    $sql .= "institucionTelefono    = '{$this->input->post('telefonos')}',";
                    $sql .= "institucionDireccion   = '{$this->input->post('direccion')}',";
                    $sql .= "institucionPagina      = '{$this->input->post('pagina')}',";
                    $sql .= "institucionContacto    = '{$this->input->post('contacto')}',";
                    $sql .= "institucionTipoFormacion = '{$this->input->post('formacion')}',";
                    $sql .= "institucionEnfoquesFormacion = '{$this->input->post('enfoque')}',";
                    $sql .= "ciudadId = '{$this->input->post('ciudad')}' where institucionId = '{$this->input->post('instiId')}'";
                    $this->db->query($sql);
                    break;
                case '4'://Persona
                    $tran = $this->input->post("transaccion");
                    $sql = "update personas set ";
                    $sql .= "personaDocumento = '{$this->input->post('nit')}',";
                    $sql .= "personaTipoDocumento = '{$this->input->post('tipDoc')}',";
                    $sql .= "personaCompetencias = '{$this->input->post('competencias')}',";
                    $sql .= "personaNivelAcademico = '{$this->input->post('nivelAca')}',";
                    $sql .= "personaNombres = '{$this->input->post('nombre')}',";
                    $sql .= "personaApellidos = '{$this->input->post('apellido')}',";
                    $sql .= "personaGenero = '{$this->input->post('genero')}',";
                    $sql .= "personaFechaNac = '{$this->input->post('fecNac')}',";
                    $sql .= "personaEstadoCivil = '{$this->input->post('estadoCivil')}',";
                    $sql .= "personaPersonaACargo = '{$this->input->post('perCargo')}',";
                    $sql .= "personaEstrato = '{$this->input->post('estrato')}',";
                    $sql .= "personaAspiracion = '{$this->input->post('aspiracion')}',";
                    $sql .= "ciudadId = '{$this->input->post('ciudad')}' where personasId = '{$this->input->post('personaId')}'";
                    $this->db->query($sql);
                    $personaId = $this->input->post('personaId');
                    //Revisa si ingreso experiencia Laboral
                    if ($this->countTemporal("exp", $tran) > 0) {
                        $sql = "delete from explaboral where personasId = '$personaId'";
                        $this->db->query($sql);
                        $sql = "select tempVarchar1,tempVarchar2,tempVarchar3,tempVarchar4,tempDate1,tempDate2,tempText1 from temporal where transaccion = '$tran' and tempInd = 'exp'";
                        $query = $this->db->query($sql);
                        foreach ($query->result() as $value) {
                            $sql = "insert into explaboral(expLaboralEmpresa,expLaboralContacto,expLaboralTelefono,expLaboralInicio,expLaboralFin,expLaboralFunciones,expLaboralActual,personasId) values (";
                            $sql .= "'{$value->tempVarchar1}','{$value->tempVarchar3}','{$value->tempVarchar4}','{$value->tempDate1}','{$value->tempDate2}','{$value->tempText1}','{$value->tempVarchar2}','$personaId')";
                            $this->db->query($sql);
                        }
                    }
                    //Revisa si ingreso Información Académica
                    if ($this->countTemporal("aca", $tran) > 0) {
                        $sql = "delete from formacademicas where personasId = '$personaId'";
                        $this->db->query($sql);
                        $sql = "select tempVarchar1,tempVarchar2,tempVarchar4,tempVarchar3,tempVarchar5,tempDate1,tempDate2 from temporal where transaccion = '$tran' and tempInd = 'aca'";
                        $query = $this->db->query($sql);
                        foreach ($query->result() as $value) {
                            $sql = "insert into formacademicas(formAcademicaTipo,tipFormacionId,formAcademicaInstitucion,formAcademicaTitulo,formAcademicaInicio,formAcademicaFin,formAcademicaActual,personasId) values (";
                            $sql .= "'{$value->tempVarchar2}','{$value->tempVarchar3}','{$value->tempVarchar1}','{$value->tempVarchar4}','{$value->tempDate1}','{$value->tempDate2}','{$value->tempVarchar5}','$personaId')";
                            $this->db->query($sql);
                        }
                    }
                    //Revisa si ingreso Discapacidades
                    if ($this->countTemporal("discapacidad", $tran) > 0) {
                        $sql = "delete from discapacidades where personasId = '$personaId'";
                        $this->db->query($sql);
                        $sql = "select tempVarchar1,tempText1,tempText2 from temporal where transaccion = '$tran' and tempInd = 'discapacidad'";
                        $query = $this->db->query($sql);
                        foreach ($query->result() as $value) {
                            $sql = "insert into discapacidades(discapacidadDesc,tipDiscapacidadId,discapacidadCausa,personasId) values (";
                            $sql .= "'{$value->tempText1}','{$value->tempVarchar1}','{$value->tempText2}','$personaId')";
                            $this->db->query($sql);
                        }
                    }
                    //Revisa si ingreso Ayudas Tecnicas
                    if ($this->countTemporal("ayudasTecnicas", $tran) > 0) {
                        $sql = "delete from ayudastecnicas where personasId = '$personaId'";
                        $this->db->query($sql);
                        $sql = "select tempVarchar2,tempText1 from temporal where transaccion = '$tran' and tempInd = 'ayudasTecnicas'";
                        $query = $this->db->query($sql);
                        foreach ($query->result() as $value) {
                            $sql = "insert into ayudastecnicas(tipAyudaTecnicaId,ayudaTecnicaComentario,personasId) values (";
                            $sql .= "'{$value->tempVarchar2}','{$value->tempText1}','$personaId')";
                            $this->db->query($sql);
                        }
                    }
                    //Revisa si ingreso Seguridad Social
                    if ($this->countTemporal("administraPersonas", $tran) > 0) {
                        $sql = "delete from adminispersona where personasId = '$personaId'";
                        $this->db->query($sql);
                        $sql = "select tempInt1,tempVarchar2,tempDate1,tempDate2,tempVarchar3 from temporal where transaccion = '$tran' and tempInd = 'administraPersonas'";
                        $query = $this->db->query($sql);
                        foreach ($query->result() as $value) {
                            $sql = "insert into adminispersona(administradoraId,adminPersonaContrato,adminPersonaFecIni,adminPersonaFecFin,adminPersonaActual,personasId) values (";
                            $sql .= "'{$value->tempInt1}','{$value->tempVarchar2}','{$value->tempDate1}','{$value->tempDate2}','{$value->tempVarchar3}','$personaId')";
                            $this->db->query($sql);
                        }
                    }
                    break;
            }
        }
        $this->db->trans_complete();
    }

    public function buscaAcademico() {
        $sql = "select * from tipformacion order by tipFormacionId";
        return $this->db->query($sql);
    }

    public function guardaExpLab($opc, $tran = NULL) {
        $tran = (isset($tran)) ? $tran : $this->input->post('transaccion');
        switch ($opc) {
            case 'C':
                $funciones = trim($this->input->post('funciones'));
                $sql = "insert into temporal(tempVarchar1,tempVarchar2,tempVarchar3,tempVarchar4,tempDate1,tempDate2,tempText1,transaccion,tempInd) values(";
                $sql .= "'{$this->input->post('nombre')}','{$this->input->post('actual')}','{$this->input->post('contacto')}','{$this->input->post('tel')}','{$this->input->post('fecIni')}','{$this->input->post('fecFin')}','$funciones','{$this->input->post('transaccion')}','exp')";
                $this->db->query($sql);
                return;
                break;
            case 'E':
                $sql = "select tempVarchar1,tempVarchar2,tempVarchar3,tempVarchar4,tempDate1,tempDate2,tempText1,tempId from temporal where tempId = '$tran'";
                $query = $this->db->query($sql);
                $obj = $query->result();
                echo json_encode($obj[0]);
                break;
            case 'A': //actualiza el registro
                $funciones = trim($this->input->post('funciones'));
                $sql = "update temporal set tempVarchar1 = '{$this->input->post('nombre')}',";
                $sql .= "tempVarchar2 = '{$this->input->post('actual')}',";
                $sql .= "tempVarchar3 = '{$this->input->post('contacto')}',";
                $sql .= "tempVarchar4 = '{$this->input->post('tel')}',";
                $sql .= "tempDate1    = '{$this->input->post('fecIni')}',";
                $sql .= "tempDate2    = '{$this->input->post('fecFin')}',";
                $sql .= "tempText1    = '$funciones' where tempId = '{$this->input->post('tempId')}'";
                $this->db->query($sql);
                return;
                break;
            case 'D': //actualiza el registro
                $sql = "delete from temporal where tempId = '$tran'";
                $this->db->query($sql);
                break;
            default:
                break;
        }
    }

    public function estudios($opc, $tran = NULL) {
        $tran = (isset($tran)) ? $tran : $this->input->post('transaccion');
        switch ($opc) {
            case 'C':
                $titulo = trim($this->input->post('titulo'));
                $institucion = trim($this->input->post('institucion'));
                $sql = "insert into temporal(tempVarchar1,tempVarchar2,tempVarchar3,tempVarchar4,tempVarchar5,tempDate1,tempDate2,transaccion,tempInd) values(";
                $sql .= "'$institucion','{$this->input->post('tipo')}','{$this->input->post('formacion')}','$titulo','{$this->input->post('actual')}','{$this->input->post('fecIni')}','{$this->input->post('fecFin')}','{$this->input->post('transaccion')}','aca')";
                $this->db->query($sql);
                break;
            case 'E':
                $sql = "select tempVarchar1,tempVarchar2,tempVarchar3,tempVarchar4,tempVarchar5,tempDate1,tempDate2,tempId from temporal where tempId = '$tran'";
                $query = $this->db->query($sql);
                $obj = $query->result();
                echo json_encode($obj[0]);
                break;
            case 'A': //actualiza el registro
                $titulo = trim($this->input->post('titulo'));
                $institucion = trim($this->input->post('institucion'));
                $sql = "update temporal set tempVarchar1 = '$institucion',";
                $sql .= "tempVarchar2 = '{$this->input->post('tipo')}',";
                $sql .= "tempVarchar3 = '{$this->input->post('formacion')}',";
                $sql .= "tempVarchar4 = '$titulo',";
                $sql .= "tempVarchar5 = '{$this->input->post('actual')}',";
                $sql .= "tempDate1    = '{$this->input->post('fecIni')}',";
                $sql .= "tempDate2    = '{$this->input->post('fecFin')}' where tempId = '{$this->input->post('tempId')}'";
                $this->db->query($sql);
                break;
            case 'D': //actualiza el registro
                $sql = "delete from temporal where tempId = '$tran'";
                $this->db->query($sql);
                break;
            default:
                break;
        }
    }

    public function discapacidad($opc, $tran = NULL) {
        $tran = (isset($tran)) ? $tran : $this->input->post('transaccion');
        switch ($opc) {
            case 'C':
                $disca = trim($this->input->post('descripcion'));
                $causa = trim($this->input->post('causa'));
                $sql = "insert into temporal(tempVarchar1,tempText1,tempText2,transaccion,tempInd) values(";
                $sql .= "'{$this->input->post('tipo')}','$disca','$causa','{$this->input->post('transaccion')}','discapacidad')";
                $this->db->query($sql);
                return;
                break;
            case 'E':
                $sql = "select tempVarchar1,tempVarchar2,tempText1,tempText2,transaccion,tempId from temporal where tempId = '$tran'";
                $query = $this->db->query($sql);
                $obj = $query->result();
                echo json_encode($obj[0]);
                return;
                break;
            case 'A': //actualiza el registro
                $disca = trim($this->input->post('descripcion'));
                $causa = trim($this->input->post('causa'));
                $sql = "update temporal set tempVarchar1 = '{$this->input->post('tipo')}',";
                $sql .= "tempText1 = '$disca',";
                $sql .= "tempText2 = '$causa' where tempId = '{$this->input->post('tempId')}'";
                $this->db->query($sql);
                break;
            case 'D': //actualiza el registro
                $sql = "delete from temporal where tempId = '$tran'";
                $this->db->query($sql);
                break;
            default:
                break;
        }
    }

    public function ayudasTec($opc, $tran = NULL) {
        $tran = (isset($tran)) ? $tran : $this->input->post('transaccion');
        switch ($opc) {
            case 'C':
                $obs = trim($this->input->post('observacion'));
                $sql = "insert into temporal(tempVarchar1,tempVarchar2,tempText1,transaccion,tempInd) values(";
                $sql .= "'{$this->input->post('clases')}','{$this->input->post('ayuda')}','$obs','{$this->input->post('transaccion')}','ayudasTecnicas')";
                $this->db->query($sql);
                break;
            case 'E':
                $sql = "select tempVarchar1,tempVarchar2,tempVarchar3,tempVarchar4,tempText1,transaccion,tempId from temporal where tempId = '$tran'";
                $query = $this->db->query($sql);
                $obj = $query->result();
                echo json_encode($obj[0]);
                break;
            case 'A': //actualiza el registro
                $obs = trim($this->input->post('observacion'));
                $sql = "update temporal set tempVarchar1 = '{$this->input->post('clases')}',";
                $sql .= "tempVarchar2 = '{$this->input->post('ayuda')}',";
                $sql .= "tempText1 = '$obs' where tempId = '{$this->input->post('tempId')}'";
                $this->db->query($sql);
                break;
            case 'D': //actualiza el registro
                $sql = "delete from temporal where tempId = '$tran'";
                $this->db->query($sql);
                break;
            default:
                break;
        }
    }

    public function adminSalud($opc, $tran = NULL) {
        $tran = (isset($tran)) ? $tran : $this->input->post('transaccion');
        switch ($opc) {
            case 'C':
                $cont = trim($this->input->post('contrato'));
                $sql = "insert into temporal(tempVarchar1,tempInt1,tempVarchar2,tempVarchar3,tempDate1,tempDate2,transaccion,tempInd) values(";
                $sql .= "'{$this->input->post('tipo')}','{$this->input->post('adminis')}','$cont','{$this->input->post('actual')}','{$this->input->post('fecIni')}','{$this->input->post('fecFin')}','{$this->input->post('transaccion')}','administraPersonas')";
                $this->db->query($sql);
                break;
            case 'E':
                $sql = "select tempVarchar1,tempVarchar2,tempVarchar3,tempInt1,tempDate1,tempDate2,transaccion,tempId from temporal where tempId = '$tran'";
                $query = $this->db->query($sql);
                $obj = $query->result();
                echo json_encode($obj[0]);
                break;
            case 'A': //actualiza el registro
                $cont = trim($this->input->post('contrato'));
                $sql = "update temporal set tempVarchar1 = '{$this->input->post('tipo')}',";
                $sql .= "tempInt1 = '{$this->input->post('adminis')}',";
                $sql .= "tempVarchar2 = '$cont',";
                $sql .= "tempVarchar3 = '{$this->input->post('actual')}',";
                $sql .= "tempDate1 = '{$this->input->post('fecIni')}',";
                $sql .= "tempDate2 = '{$this->input->post('fecFin')}' where tempId = '{$this->input->post('tempId')}'";
                $this->db->query($sql);
                break;
            case 'D': //actualiza el registro
                $sql = "delete from temporal where tempId = '$tran'";
                $this->db->query($sql);
                break;
            default:
                break;
        }
    }

    public function countTemporal($opc, $transaccion) {
        $sql = "select count(transaccion) as total from temporal where tempInd = '$opc' and transaccion = '$transaccion'";
        $query = $this->db->query($sql);
        $obj = $query->result();
        return $obj[0]->total;
    }

}

?>
